package antlr4.definitive.d3_arrayinit.d3_1_arrayinit.run;

import antlr4.definitive.d3_arrayinit.d3_1_arrayinit.gen.ArrayInitLexer;
import antlr4.definitive.d3_arrayinit.d3_1_arrayinit.gen.ArrayInitParser;
import lombok.SneakyThrows;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.Scanner;

public class Main {
    @SneakyThrows
    public static void main(String[] args) {
        //新建一个CharStream，从标准输入读取数据
//        ANTLRInputStream input = new ANTLRInputStream(System.in);//失效更新
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个 数组初始化格式的串 如：{1,2,3}");
        String input = scanner.nextLine();
        CodePointCharStream codePointCharStream = CharStreams.fromString(input);
        //新建一个词法分析器，处理输入的CharStream
        ArrayInitLexer lexer = new ArrayInitLexer(codePointCharStream);
        //新建一个词法符号的缓冲区，用于存储词法分析器将生成的词法符号
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        //新建一个语法分析器，处理词法符号缓冲区中的内容
        ArrayInitParser parser = new ArrayInitParser(tokens);
        ParseTree tree = parser.init();//针对init规则，开始语法分析
        System.out.println(tree.toStringTree(parser));//用LIsP风格打印生成的树
    }
}

























